package com.imooc.springboot.dubbo.nilinili.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.nilinili.consumer.utils.JwtToUserId;
import com.imooc.springboot.dubbo.nilinili.entity.dto.LoginUserDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.UserDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.editUserDTO;
import com.imooc.springboot.dubbo.nilinili.service.CollectionService;
import com.imooc.springboot.dubbo.nilinili.service.PhotoService;
import com.imooc.springboot.dubbo.nilinili.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
@Validated
public class UserController {

    @Reference
    private UserService userService;

    @Reference
    private PhotoService photoService;

    @Reference
    private CollectionService collectionService;

    /**
     * 用户登录
     * @param loginUserDTO
     * @return
     */
    @PostMapping("/login")
    public String login(@Validated @RequestBody LoginUserDTO loginUserDTO){
        return userService.userLogin(loginUserDTO);
    }

    /**
     * 用户退出
     * @param token
     * @return
     */
    @PostMapping("/logout")
    public String logout(@RequestHeader("token") String token){
        return userService.userLogout(token);
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/getUserInfo")
    public String getUserInfo(@RequestHeader("token") String token){
        String s = JwtToUserId.Jwt2UserId(token);
        photoService.getAllPhotoCounts(s);
        photoService.getAllPhotoApprovalCounts(s);
        collectionService.countCollection(s);
        return userService.getUserInfo(token);
    }

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public String register(@Validated @RequestBody UserDTO userDTO){
        return userService.userRegister(userDTO);
    }

    /**
     * 检测用户是否已经登录
     * @param token
     * @return
     */
    @GetMapping("/checkLogin")
    public String checkLogin(@RequestHeader("token") String token){
        return userService.checkLogin(token);
    }

    /**
     * 修改用户个人资料方法
     * @param token
     * @param editUserDTO
     * @return
     */
    @PostMapping("/editUser")
    public String editUser(@RequestHeader("token") String token,@Validated @RequestBody editUserDTO editUserDTO){
        editUserDTO.setToken(token);
        return userService.editUser(editUserDTO);
    }


    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfoByUserId")
    @Transactional
    public String getUserInfoByUserId(@RequestParam("userId") String userId){
        photoService.getAllPhotoCounts(userId);
        photoService.getAllPhotoApprovalCounts(userId);
        return userService.getUserInfoByUserId(userId);
    }
}
